package com.guanglan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guanglan.entity.SysPermission;
import com.guanglan.service.SysPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 作者: 光蓝Comet
 * Date: 2018-06-20 15:29
 * Description: 一句话说明这个类的作用
 * Since 1.0
 */
@RequestMapping(value = "/system/perm")
@Controller
public class SysPermController {

    private static final Logger logger = LoggerFactory.getLogger(SysPermController.class);

    @Autowired
    private SysPermissionService sysPermissionService;

    @RequestMapping(value = "/index")
    public String permIndex(@RequestParam(name = "menuName",defaultValue = "",required = false) String menuName,
                            @RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,
                            @RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize,
                            Model model){
        logger.info("Current user into the user method!");
        PageHelper.startPage(nowPage, pageSize);
        PageHelper.orderBy("create_time desc");
        List<SysPermission> perms =  sysPermissionService.selectSysPermissionsByMenuName(menuName);
        PageInfo<SysPermission> pageInfo = new PageInfo<>(perms);
        model.addAttribute("pageInfo",pageInfo);
        return "perm/permIndex";
    }
    @RequestMapping(value = "/editPage")
    public String editPage(@RequestParam(value="editId",required=false) String editId,Model model){
        if (StringUtils.trimToNull(editId) != null){
            SysPermission sysPermission = sysPermissionService.selectByPrimaryKey(Long.valueOf(editId));
            model.addAttribute("perm",sysPermission);
        }
        return "perm/permEdit";
    }

    @RequestMapping(value = "/editPerm")
    @ResponseBody
    public String editPerm(@RequestParam(value="editId",required=false) String editId,
                           @RequestParam(name = "menuName",defaultValue = "",required = false) String menuName,
                           @RequestParam(name = "permission",defaultValue = "",required = false) String permission,
                           @RequestParam(name = "menuUrl",defaultValue = "",required = false) String menuUrl,
                           @RequestParam(name = "pid",defaultValue = "",required = false) long pid){

        SysPermission sysPermission = new SysPermission();
        sysPermission.setMenuName(menuName);
        sysPermission.setPermission(permission);
        sysPermission.setMenuUrl(menuUrl);
        sysPermission.setPid(pid);
        try {
            if (StringUtils.trimToNull(editId) != null){
                sysPermission.setId(Long.valueOf(editId));
                sysPermissionService.updateByPrimaryKeySelective(sysPermission);
                return "{\"msg\":\"修改权限菜单成功\"}";
            }else {
                sysPermissionService.insertSelective(sysPermission);
                return "{\"msg\":\"添加权限菜单成功\"}";
            }
        }catch (NullPointerException e){
            e.printStackTrace();
            return "{\"msg\":\"系统错误\"}";
        }
    }

    @RequestMapping(value = "/delPerm")
    @ResponseBody
    public String delPerm(@RequestParam(value="delId",required=false) Long delId){
        sysPermissionService.deleteByPrimaryKey(delId);
        return "{\"msg\":\"删除成功\"}";
    }


    @RequestMapping(value = "/getPerms")
    @ResponseBody
    public List<SysPermission> getPerms(){
      return  sysPermissionService.getPerms();
    }


}
